package com.lebang.lechuangsystem.controller.sys;


import com.lebang.lechuangsystem.domain.Permission;
import com.lebang.lechuangsystem.domain.Role;
import com.lebang.lechuangsystem.domain.RolePermission;
import com.lebang.lechuangsystem.model.PageBean;
import com.lebang.lechuangsystem.po.PermissionPo;
import com.lebang.lechuangsystem.po.RolePo;
import com.lebang.lechuangsystem.service.sys.PermissionService;
import com.lebang.lechuangsystem.service.sys.RoleService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author: hwq
 * @Date: 2019/8/22 15:12
 * @Version 1.0
 */
@Api(tags = "<后台管理> 角色管理管理")
@RestController
@RequestMapping("/sys/role")
public class RoleController {
    @Autowired
    private RoleService baseService;
    @Autowired
    private PermissionService permissionService;

    @ApiOperation(value = "添加")
    @PostMapping("/add")
    public JsonData add(Role vo) {
        vo.setCreatedTime(new Date());
        vo.setType(1);
        return baseService.insertModel(vo) > 0 ? Callback.success("200", "操作成功") : Callback.fail("200", "操作失败");
    }

    @ApiOperation(value = "修改")
    @PostMapping("/update")
    public JsonData update(Role vo) {
        vo.setUpdatedTime(new Date());
        return baseService.updateById(vo) > 0 ? Callback.success("200", "操作成功") : Callback.fail("200", "操作失败");
    }

    @ApiOperation(value = "根据id获取详情")
    @GetMapping("/get/{id}")
    public JsonData get(@PathVariable Integer id, HttpServletRequest request) {
        return Callback.success("200", "查询成功", baseService.selectById(id));
    }

    @ApiOperation(value = "根据id删除")
    @PostMapping("/delete/{id}")
    public JsonData delete(@PathVariable Integer id, HttpServletRequest request) {
        return baseService.deleteById(id) > 0 ? Callback.success("200", "操作成功") : Callback.fail("200", "操作失败");
    }

    @ApiOperation(value = "分页")
    @PostMapping("/list")
    public JsonData list(HttpServletRequest request, @RequestParam Integer page, @RequestParam Integer limit,

                         @RequestParam(required = false) String truename) {
        RolePo example = new RolePo();
        example.setOffset((page - 1) * limit.longValue());
        example.setLimit(limit);
        RolePo.Criteria criteria = example.createCriteria();
        if(!TextUtil.isEmpty(truename)){
            criteria.andNameLike("%"+truename+"%");
        }
        PageBean<Role> pageBean = baseService.pageByExample(example);
        return Callback.success("200", "查询成功", pageBean);
    }

    @ApiOperation(value = "获取角色的所有菜单")
    @PostMapping("/permission/list")
    public JsonData rolePermission(HttpServletRequest request, @RequestParam(value = "roleId") Integer roleId) {
        Role role=baseService.selectById(roleId);
        if(role==null){
            return Callback.success("200", "用户不存在");
        }
        //超级管理员默认全部菜单
        if(role.getType().equals(0)){
            roleId=null;
        }
        List<Permission> permissions = baseService.selectPermissionListByRoleId(roleId);
        return Callback.success("200", "查询成功", permissions);
    }
    @ApiOperation(value = "给角色添加菜单")
    @PostMapping("/permission/add")
    public JsonData rolePermissionAdd(HttpServletRequest request,
                                      @ApiParam(name = "角色ID",value = "roleId" ,required = true) Integer roleId,
                                      @ApiParam(name = "菜单ID多个用,隔开",value = "list" ,required = true) String list) {
        try {
//            List<Permission> permissions = baseService.selectPermissionListByRoleId(roleId);
//            List<String> checkList = new ArrayList<>();
//            permissions.forEach(s -> checkList.add(s.getId().toString()));
            List<String> stringList = Arrays.asList(list.split(","));
//            stringList = stringList.stream().distinct().filter(s -> !checkList.contains(s)).collect(Collectors.toList());

            return baseService.rolePermissionAdd(roleId, stringList) > 0 ? Callback.success("200", "操作成功") : Callback.fail("200", "操作失败");
        } catch (Exception e) {
            return Callback.fail("200", "格式错误");
        }
    }

    @PostMapping("/permission/data")
    public JsonData data(HttpServletRequest request, @RequestParam(value = "roleId") Integer roleId) {
        Role role=baseService.selectById(roleId);
        if(role==null){
            return Callback.fail("success", "角色不存在");
        }
        StringBuffer ids=new StringBuffer();
        //超级管理员默认全部菜单
        if(role.getType().equals(0)){
            PermissionPo example=new PermissionPo();
            List<Permission> permissions = permissionService.pageByExample(example).getData();
            permissions.forEach(s->ids.append(","+s.getId()));
        }else {
            List<RolePermission> permissions = baseService.selectAllList(roleId);
            permissions.forEach(s->ids.append(","+s.getPermissionId()));
        }
        ids.delete(0,1);
        return Callback.success("success", "查询成功", ids);
    }
}
